package cn.kgc.interceptor;

import cn.kgc.entity.DevUser;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 验证用户是否登录过的拦截器
 */
//@Component
public class LoginInterceptor implements HandlerInterceptor{

    //卡在控制器中的业务处理方法执行之前先执行它,返回值为true代表放行请求，返回值为false代表拦截请求
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        DevUser devUser =(DevUser) request.getSession().getAttribute("devUser");
        if (devUser == null) {
            response.sendRedirect(request.getContextPath()+"/");
            return false;
        }
        return true;
    }

}
